package vistas;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

//import nsrc.paquete14.VistaCompetencia;

import controladores.ControladorJuego;

import modelo.*;
//import 
public class VistaContenedora extends JFrame{
	
	private Usuario jugadorDeTurno;
	private PanelInfoJuego panelJugador;
	private PanelInfoPista panelPista;
	private ControladorJuego controladorJuego;
	private VentanaCompetencia VentanaComp;
	private JLabel IniciarCompetencia;
	private JLabel IniciarCompra;
	private JLabel ElegirPista;
	private JLabel Salir;
	private JLabel Error;
	
	public VistaContenedora(ControladorJuego controlJuego){
		this.controladorJuego = controlJuego;
		this.getContentPane().setBackground(Color.black); 
		this.setMaximizedBounds(new Rectangle(1000,600));
		initComponents();
	}
	

	private void initComponents(){
		this.setVisible(false);
		this.setTitle("RACING ");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new GridBagLayout());
		this.setSize(800,600);
		GridBagConstraints constraints = new GridBagConstraints();
		// Inicializo los demas componentes de la GUI
		this.inicializarPaneles(constraints);
	}
	
	private void inicializarPaneles(GridBagConstraints constraints){
		
		this.panelJugador = new PanelInfoJuego();
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		constraints.anchor = GridBagConstraints.WEST;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx = 0.5;
		constraints.weighty = 0.0;
		constraints.insets = new Insets(3,3,3,3);
		constraints.ipadx = 2;
		constraints.ipady = 2;
		this.add(this.panelJugador,constraints);
		
		
		constraints.weightx = 0.0;
		constraints.gridy = 1; // baja uno respecto del anterior panel agregado.
		this.panelPista = new PanelInfoPista();
		this.add(this.panelPista,constraints);
				
		//para las opciones		
		constraints.weightx = 0.0;
		constraints.gridy = 2;
		this.ElegirPista = new JLabel("Elegir Pista");
		this.add(this.ElegirPista,constraints);
		//this.ElegirPista.setToolTipText("Ceder el turno.");
		this.ElegirPista.setForeground(Color.blue);
		this.ElegirPista.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				controladorJuego.ElegirPista(panelPista);
			}
			public void mouseEntered(MouseEvent e){
				ElegirPista.setForeground(Color.yellow);
				
			}
			public void mouseExited(MouseEvent e){
				ElegirPista.setForeground(Color.blue);
			}		
		});
		
		constraints.weightx = 0.0;
		constraints.gridy = 3;		
		this.IniciarCompetencia= new JLabel("Iniciar Competencia");
		this.add(this.IniciarCompetencia,constraints);
		//this.ElegirPista.setToolTipText("Ceder el turno.");
		this.IniciarCompetencia.setForeground(Color.blue);
		this.IniciarCompetencia.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				//this.				
				controladorJuego.iniciarCompetencia(VentanaComp);
			}
			public void mouseEntered(MouseEvent e){
				IniciarCompetencia.setForeground(Color.yellow);				
			}
			public void mouseExited(MouseEvent e){
				IniciarCompetencia.setForeground(Color.blue);
			}		
		});
		
		//accion ELEGIR COMPONENTE
		constraints.weightx = 0.0;
		constraints.gridy = 4;
		this.IniciarCompra= new JLabel("Comprar Componente");
		this.add(this.IniciarCompra,constraints);
		//this.ElegirPista.setToolTipText("Ceder el turno.");
		this.IniciarCompra.setForeground(Color.blue);
		this.IniciarCompra.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				controladorJuego.iniciarCompra();
			}
			public void mouseEntered(MouseEvent e){
				IniciarCompra.setForeground(Color.yellow);				
			}
			public void mouseExited(MouseEvent e){
				IniciarCompra.setForeground(Color.blue);
			}		
		});
		
		//accion SALIR
		constraints.weightx = 0.0;
		constraints.gridy = 5;		
		this.Salir= new JLabel("Salir");
		this.add(this.Salir,constraints);
		//this.ElegirPista.setToolTipText("Ceder el turno.");
		this.Salir.setForeground(Color.blue);
		this.Salir.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
			public void mouseEntered(MouseEvent e){
				Salir.setForeground(Color.yellow);				
			}
			public void mouseExited(MouseEvent e){
				Salir.setForeground(Color.blue);
			}		
		});
		//para el sector de la competencia
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.gridheight = 5;
		constraints.weightx = 1.0;
		constraints.weighty = 1.0;
		//JScrollPane miScroll = new JScrollPane();
VentanaComp = new VentanaCompetencia(controladorJuego.getControlComp());
		//miScroll.setWheelScrollingEnabled(true);
		
		this.add(VentanaComp,constraints);
		this.getContentPane().add (VentanaComp, constraints);
	}
	

	public PanelInfoJuego getPanelJuego() {
		return panelJugador;
	}
	
	public void actualizarPaneles(){
		this.panelJugador.update(this.jugadorDeTurno);
	}
	
	public VentanaCompetencia getVistaCompetencia(){
		return VentanaComp;
	}

	/**
	 * Muestra el formulario.
	 */
	public void mostrar(){
		this.setVisible(true);
	}
	
	/**
	 * Ocultar el formulario.
	 */
	public void ocultar(){
		this.setVisible(false);
	}
	

	public static class CloseListener extends WindowAdapter
	{	public void windowClosing(WindowEvent e)
		{	e.getWindow().setVisible(false);
			System.exit(0);
		}
	}
	
	private void Salir(){
		System.exit(0);
	}

	public void iniciar() {
		controladorJuego.cargarJugador(this.panelJugador,this.panelPista/*,this.VentanaComp*/);
	}
}
